using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.IO;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    string cnnString = System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ConnectionString;

    public Service()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public int LicenseCount(string firmCode)
    {
        SqlConnection cn = new SqlConnection(cnnString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("SELECT     firm_limit  FROM         FIRMALAR  WHERE     (firm_code = @code)", cn);
        cmd.Parameters.AddWithValue("@code", firmCode);
        SqlDataReader rd = cmd.ExecuteReader();
        int count = 0;
        if (rd.HasRows == true)
        {
            rd.Read();
            if (rd[0] != DBNull.Value)
            {
                count = rd.GetInt32(0);
            }
        }
        rd.Close();
        cn.Close();
        return count;
    }

    [WebMethod]
    public string GenerateKey(string device_id, string firm)
    {
        string key = Security.GenerateKey(device_id);
        Kaydet(firm, device_id, key);
        return key;
    }

    void Kaydet(string firm, string device_id, string genkey)
    {
        SqlConnection cn = new SqlConnection(cnnString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("insert into LISANS (firm_id,program_id,cihaz_id,lisans_number) values (@firm_id,@program_id,@cihaz_id,@lisans_number)", cn);
        cmd.Parameters.AddWithValue("@firm_id", firm);
        cmd.Parameters.AddWithValue("@program_id", 0);
        cmd.Parameters.AddWithValue("@cihaz_id", device_id);
        cmd.Parameters.AddWithValue("@lisans_number", genkey);
        cmd.ExecuteNonQuery();
        cn.Close();
    }

    [WebMethod()]
    public Byte[] DownloadFile(string filename)
    {
        byte[] objFile = null;
        string folder = System.Configuration.ConfigurationManager.AppSettings["HelpFolder"];
        if (File.Exists(folder + filename))
        {
            Stream objStream;
            int len;
            objStream = File.Open(folder + filename, FileMode.Open, FileAccess.Read);
            len = Convert.ToInt32(objStream.Length);
            objFile = new byte[len + 1];
            objStream.Read(objFile, 0, len);
            objStream.Close();
        }
        return objFile;
    }

    [WebMethod()]
    public String[] ListFiles()
    {
        String folder = System.Configuration.ConfigurationManager.AppSettings["HelpFolder"];
        String[] files;
        files = Directory.GetFiles(folder);
        for (int i = 0; i < files.Length; i++)
        {
            files[i] = files[i].Remove(0, folder.Length);
        }
        return files;
    }

    [WebMethod]
    public string GetRandomKey(string customerNo)
    {
        Random rnd = new Random();
        int key = rnd.Next(1000, 9999);

        SqlConnection cn = new SqlConnection(cnnString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("insert into RANDOM_KEY (generate_key) values (@generate_key)", cn);
        cmd.Parameters.AddWithValue("@generate_key", key);
        cmd.ExecuteNonQuery();
        cn.Close();

        return key.ToString();
    }

    [WebMethod]
    public string RemoveRandomKey(int key)
    {
        SqlConnection cn = new SqlConnection(cnnString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("delete from RANDOM_KEY  where generate_key = @generate_key ", cn);
        cmd.Parameters.AddWithValue("@generate_key", key);
        cmd.ExecuteNonQuery();
        cn.Close();
        return "ok";
    }

}

